package com.qf.service.impl;



import com.qf.mapper.TeacherMapper;

import com.qf.pojo.Student;
import com.qf.pojo.Teacher;
import com.qf.service.TeacherService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherMapper teacherMapper;

    @Override
    public ResultMsg selectAllTeacher( Integer teacherId, Integer teacherStatus , Integer page, Integer limit) {
        page = page <= 1 ? 1 : page;
        teacherId = teacherId == -1 ? null : teacherId;
        teacherStatus = teacherStatus == -1 ? null : teacherStatus;
        List<Teacher> teachers = teacherMapper.selectAllTeacher(teacherId, teacherStatus, (page - 1) * limit, limit);
        if (teachers.size() == 0) return ResultMsg.FAILED(201, "no data was found");
        return ResultMsg.SUCCESS(0,teachers, teacherMapper.selectTeacherCount().getTeacherId());
    }

    @Override
    public ResultMsg selectOneTeacher(Integer teacherId) {
        if (teacherId == null) return ResultMsg.FAILED(202, "roleId can not be null");
        List<Teacher> teachers = teacherMapper.selectTeacher(teacherId);
        if (teachers.size() != 1) return ResultMsg.FAILED(201, "no data was found");
        return ResultMsg.SUCCESS(teachers.get(0));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMsg updateTeacher(Teacher teacher) {
        if (teacherMapper.updateTeacher(teacher) == 0)
            return ResultMsg.FAILED(201, "修改失败");
        return ResultMsg.SUCCESS();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMsg insertTeacher(Teacher teacher) {
        if(teacher.getTeacherId()==null)
            return ResultMsg.FAILED(202, "参数不能为空");
        if (teacherMapper.insertTeacher(teacher) == 0)
            return ResultMsg.FAILED(201, "新增失败");
        return ResultMsg.SUCCESS(0, teacher, 0);
    }

    @Override
    public ResultMsg updateTeacherStatus(List<Integer> teacherIds, Integer teacherStatus) {
        if (teacherStatus != 0 && teacherStatus != 1)
            return ResultMsg.FAILED(206, "The userStatus parameter does not allow this value");
        int i = teacherMapper.updateTeacherStatus(teacherIds, teacherStatus);
        return new ResultMsg(0, i + " rows of data are successfully modified", null, 0);
    }


    @Override
    public ResultMsg deleteTeacher(List<Integer> teacherIds) {
        int i = teacherMapper.deleteTeacher(teacherIds);
        if (i > 1)
            return new ResultMsg(0, i + " rows of data are deleted successfully", null, 0);
        else if (i==0)
            return new ResultMsg(201, i + " rows of data are deleted failed", null, 0);
        else
            return new ResultMsg(0, i + " row of data is deleted successfully", null, 0);
    }

}
